﻿using System.Text;
using System;
namespace CommLib
{
    public class Conversation
    {
        public readonly static int FIELD_COUNT = 5;

	    private readonly string mPhoneNumber;
	    private readonly string mContactName;
	    private readonly int mThreadId;
	    private readonly int mMsgCount;
	    private readonly string mSnippet;

	    public Conversation(string number, string name, int threadId, int msgCount,
			    string snippet) 
        {
		    mPhoneNumber = number;
		    mContactName = name;
		    mThreadId = threadId;
		    mMsgCount = msgCount;
		    mSnippet = snippet;
	    }

	    public Conversation(string[] data, int firstIdx) 
        {
		    if (data.Length - firstIdx < FIELD_COUNT)
			    throw new ArgumentException("Too few data fields!");

		    mPhoneNumber = data[firstIdx++];
		    mContactName = data[firstIdx++];
		    mThreadId = int.Parse(data[firstIdx++]);
		    mMsgCount = int.Parse(data[firstIdx++]);
		    mSnippet = data[firstIdx++];
	    }

	    public string getPhoneNumber() {
		    return mPhoneNumber;
	    }

	    public string getContactName() {
		    return mContactName;
	    }

	    public int getThreadId() {
		    return mThreadId;
	    }

	    public int getMessageCount() {
		    return mMsgCount;
	    }

	    public string getSnippet() {
		    return mSnippet;
	    }

	    public string toTitlestring() 
        {
		    if (mContactName == null || mContactName.Equals(""))
			    return mPhoneNumber;
		    else
			    return mContactName;
	    }

	    public override string ToString() 
        {
            if (mSnippet != null)
                return toStringNoSnippet() + " " + mSnippet;
            else
                return toStringNoSnippet();
	    }

        public string toStringNoSnippet()
        {
            StringBuilder b = new StringBuilder();
            b.Append(toTitlestring());
            b.Append(" [");
            b.Append(mMsgCount);
            b.Append("]");

            return b.ToString();
        }
    }
}
